package com.tool.pet;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.tool.role.QualityZW;
import org.come.Jpanel.ZhuJpanel;
import org.come.entity.Goodstable;
import org.come.entity.Mount;
import org.come.entity.MountSkill;
import org.come.entity.Pal;
import org.come.entity.RoleSummoning;
import org.come.model.PalData;
import org.come.mouslisten.PetAddPointMouslisten;
import org.come.until.AccessNedanMsgUntil;
import org.come.until.AnalysisString;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.JmSum;
import org.come.until.Util;

import com.tool.btn.PetPanelBtn;
import com.tool.role.Ql;
import com.tool.role.RoleData;

public class PetProperty {
	static String[] evs=new String[]{"根骨=","灵性=","力量=","敏捷=","炼化属性","增加气血","增加法力","增加攻击"};
	private static Ql ql;
	public static Ql getQl(RoleSummoning pet) {
		if (ql==null) {
            ql=new Ql();
        }
		return AccessNedanMsgUntil.getPetQl(ql,pet);
	}
	public static Ql getQl(Pal pal) {
		if (ql==null) {
            ql=new Ql();
        }
		return AccessNedanMsgUntil.getPalQl(ql,pal);
	}
	public static void ShowQl(RoleSummoning pet){
		PetPanelBtn.showRolesumming(getQl(pet));
	}
	public static void ShowQl(Pal pal){
		PetPanelBtn.showRolesumming(getQl(pal));
	}
	/**获取伙伴的hp,mp,ap,sp*/
	public static int[] getPalHMASp(Pal pal,PalData palData){
		int[] pals=new int[4];
		double grow=pal!=null?pal.getGrow():1D;
        Integer grade = RoleData.getRoleData().getLoginResult().getGrade();
        int lvl = AnalysisString.lvlint(grade);
        int zs = AnalysisString.lvltrue(grade);
		if (pal!=null) {
			Map<String, Double> map=AccessNedanMsgUntil.getPalMap(pal, true);
			pals[0]=getBase(lvl, (int)AccessNedanMsgUntil.getMapValue(map, "根骨"), grow, palData.getHp(), 0, map);
			pals[1]=getBase(lvl, (int)AccessNedanMsgUntil.getMapValue(map, "灵性"), grow, palData.getMp(), 1, map);
			pals[2]=getBase(lvl, (int)AccessNedanMsgUntil.getMapValue(map, "力量"), grow, palData.getAp(), 2, map);
			pals[3]=getBase(lvl, (int)AccessNedanMsgUntil.getMapValue(map, "敏捷"), grow, palData.getSp(), 3, map);
			map.clear();
		}else {
			int[] jds=palData.getJds();
			int size=jds[0]+jds[1]+jds[2]+jds[3];
			int point=lvl+zs*40;//分配点数
			point=lvl%size;
			for (int i = 0; i < pals.length; i++) {
				pals[i]+=lvl+point/size*jds[i];
				if (point>0&&jds[i]>0) {
					if (point>palData.getJds()[i]) {
						pals[i]+=jds[i];
						point-=jds[i];
					}else {
						pals[i]+=point;
						point=0;
					}
				}
			}
			pals[0]=getRoleValue(lvl, pals[0], grow, palData.getHp(), 0);
			pals[1]=getRoleValue(lvl, pals[1], grow, palData.getMp(), 1);
			pals[2]=getRoleValue(lvl, pals[2], grow, palData.getAp(), 2);
			pals[3]=getRoleValue(lvl, pals[3], grow, palData.getSp(), 3);
		}
		return pals;
	}
	public static int getBase(int lvl,int P,double G, int base, int type,Map<String,Double> map){
		int value=getRoleValue(lvl,P,G,base,type);
		if (type==0) {
			value+=AccessNedanMsgUntil.getMapValue(map, "hp");
			value+=AccessNedanMsgUntil.getMapValue(map, "HP");
			value+=AccessNedanMsgUntil.getMapValue(map, "气血");
			value+=AccessNedanMsgUntil.getMapValue(map, "加气血");
			value+=AccessNedanMsgUntil.getMapValue(map, "附加气血");
			value*=(AccessNedanMsgUntil.getMapValue(map, "HP成长")+1);
			value*=(AccessNedanMsgUntil.getMapValue(map, "加强气血")/100+1);
		}else if (type==1) {
			value+=AccessNedanMsgUntil.getMapValue(map,"mp");
			value+=AccessNedanMsgUntil.getMapValue(map,"MP");
			value+=AccessNedanMsgUntil.getMapValue(map,"法力");
			value+=AccessNedanMsgUntil.getMapValue(map,"加法力");
			value+=AccessNedanMsgUntil.getMapValue(map,"加魔法");
			value+=AccessNedanMsgUntil.getMapValue(map,"附加法力");
			value*=(AccessNedanMsgUntil.getMapValue(map,"MP成长")+(AccessNedanMsgUntil.getMapValue(map,"加强法力")/100)+1);
		}else if (type==2) {
			value+=AccessNedanMsgUntil.getMapValue(map,"ap");
			value+=AccessNedanMsgUntil.getMapValue(map,"AP");
			value+=AccessNedanMsgUntil.getMapValue(map,"攻击");
			value+=AccessNedanMsgUntil.getMapValue(map,"加攻击");
			value+=AccessNedanMsgUntil.getMapValue(map,"附加攻击");
			value*=(AccessNedanMsgUntil.getMapValue(map,"AP成长")+(AccessNedanMsgUntil.getMapValue(map,"加强攻击")/100)+1);
		}else if (type==3) {
			value+=AccessNedanMsgUntil.getMapValue(map,"sp");
			value+=AccessNedanMsgUntil.getMapValue(map,"SP");
			value+=AccessNedanMsgUntil.getMapValue(map,"速度");
			value+=AccessNedanMsgUntil.getMapValue(map,"加速度");
			value+=AccessNedanMsgUntil.getMapValue(map,"附加速度");
			value*=(AccessNedanMsgUntil.getMapValue(map,"SP成长")+(AccessNedanMsgUntil.getMapValue(map,"加强速度")/100)+1);
		}
		return value;
	} 
	/**获取召唤兽hp mp ap sp*/
	public static int[] getPetHMASp(RoleSummoning pet){
		int[] pets=new int[5];
		if (pet == null) {
			return pets;
		}
		int lvl=AnalysisString.petLvlint(pet.getGrade());
		double grow=Double.valueOf(pet.getGrowlevel());
		pets[0]=pet.getHp();
		pets[1]=pet.getMp();
		pets[2]=pet.getAp();
		pets[3]=pet.getSp();
		pets[4]=0;
		if (grow>=99.99||pets[0]>99999||pets[1]>99999||pets[2]>99999||pets[3]>99999) {
			JmSum.xiugaiqi();//召唤兽属性上限
		}
		int zBone=pet.getBone();
		int zSpir=pet.getSpir();
		int zPower=pet.getPower();
		int zSpeed=pet.getSpeed();
		int zCalm=pet.getCalm();
		int addhp=0,addmp=0,addap=0;
		
		if (pet.getStye()!=null&&pet.getStye().length()>1) {
			String[] v=pet.getStye().split("\\|");
			for (int i = 1; i < v.length; i++) {
				String[] vs=v[i].split("-");
				if (vs.length>=2) {
					Goodstable goodstable=GoodsListFromServerUntil.fushis.get(new BigDecimal(vs[1]));
					if (goodstable!=null) {
						String[] t=goodstable.getValue().split("\\|");
						for (int j = 0; j < t.length; j++) {
							if (t[j].startsWith(evs[0])) {
								String[] ts = t[j].split("=");
								zBone += Integer.parseInt(ts[1]);
							}else if (t[j].startsWith(evs[1])) {
								String[] ts = t[j].split("=");
								zSpir += Integer.parseInt(ts[1]);
							}else if (t[j].startsWith(evs[2])) {
								String[] ts = t[j].split("=");
								zPower += Integer.parseInt(ts[1]);
							}else if (t[j].startsWith(evs[3])) {
								String[] ts = t[j].split("=");
								zSpeed += Integer.parseInt(ts[1]);
							}else if (t[j].startsWith(evs[4])) {
								String[] vStrings =t[j].split("\\&");
								for (int k = 1; k < vStrings.length; k++) {
									if (vStrings[k].startsWith(evs[5])) {
										String[] mes = vStrings[k].split("=");
										addhp+=Double.parseDouble(mes[1]);	
									}else if (vStrings[k].startsWith(evs[6])) {
										String[] mes = vStrings[k].split("=");
										addmp+=Double.parseDouble(mes[1]);	
									}else if (vStrings[k].startsWith(evs[7])) {
										String[] mes = vStrings[k].split("=");
										addap+=Double.parseDouble(mes[1]);		
									}
								}
							}
						}
					}
				}
			}
		}
		pets[0]=getRoleValue(lvl,zBone ,grow,pets[0],0)+addhp;
		pets[1]=getRoleValue(lvl,zSpir ,grow,pets[1],1)+addmp;
		pets[2]=getRoleValue(lvl,zPower,grow,pets[2],2)+addap;
		pets[3]=getRoleValue(lvl,zSpeed,grow,pets[3],3);
		pets[4]=getRoleValue(lvl,zCalm ,grow,pets[4],4);
		//获取四维的数值加成
		pet.getSI(pets);
		//获取灵犀加成
		pet.getLX(pets);
		if(pet.getPetSkills()!=null) {
			if(pet.getPetSkills().contains("1248")) {
				pets[2]*=1.3;
			}
		}
		for (int i = 0; i < ZhuJpanel.getListMount().size(); i++) {//坐骑加成属性
			Mount mount=ZhuJpanel.getListMount().get(i);
			if(mount.isID(pet.getSid())){
				List<MountSkill> mountSkills=mount.getMountskill();
				PetAddPointMouslisten.shouhuvalue(pets,pet);
				if (mountSkills!=null) {
					double xs1=1,xs2=1,xs3=1,xs4=1;
					for (int j = 0; j < mountSkills.size(); j++) {
						String ms=Util.calculateAdditionBase(mount,mountSkills.get(j).getSkillname());
						if (ms!=null) {
							String v1[] =ms.split("=");
							if ("HP".equals(v1[0])){
								xs1+=Double.parseDouble(v1[1]);
							}else if ("MP".equals(v1[0])) {
								xs2+=Double.parseDouble(v1[1]);
							}else if ("AP".equals(v1[0])) {
								xs3+=Double.parseDouble(v1[1]);
							}else if ("SP".equals(v1[0])) {
								xs4+=Double.parseDouble(v1[1]);
							}	
						} 	
					}
					pets[0]*=xs1;
					pets[1]*=xs2;
					pets[2]*=xs3;
					pets[3]*=xs4;
				}
				break;
			}
		}
		return pets;
	}
	public static int getRoleValue(int lvl, int P, double G, int base, int type) {
		if (type == 0 || type == 1) {
			return (int) (lvl * P * G) + (int) ((0.7 * lvl * G + 1) * base);
		} else if (type == 2) {
			return (int) (lvl * P * G / 5)
					+ (int) ((0.14 * lvl * G + 1) * base);
		}else if (type == 3) {
			return (int) ((base + P) * G);
		}else {
			return P;
		}
	}


	public static void updateProperty(Ql ql,RoleSummoning pet, int[] pets) {
		if(pet.getXinyuan()!=null&&pet.getXinyuan().split("&").length>1){
			String[] split = pet.getXinyuan().split("&")[1].split("\\|");
			for(String skill:split){
				if("25001".equals(skill)&&pets!=null){
					//腾云 增加100速度
					pets[3]+=100;
				}else if("25002".equals(skill)&&pets!=null){
					//破云攻击力+10000
					pets[2]+=10000;
				}else if("25005".equals(skill)&&ql!=null){
					QualityZW.insertValues(ql,"连击率",15);
				}else if("25006".equals(skill)&&ql!=null){
					QualityZW.insertValues(ql,"致命率",15);
				}else if("25009".equals(skill)&&ql!=null){
					QualityZW.insertValues(ql,"忽视防御几率",15);
				}else if("25010".equals(skill)&&ql!=null){
					QualityZW.insertValues(ql,"忽视防御程度",15);
				}
			}
		}
	}
}
